# from PySide6.QtCharts import *
from PySide6 import QtCore, QtWidgets,QtGui,QtCharts
import random
class MyChartView(QtCharts.QChartView):
    def __init__(self,chart,parent=None) :
        super().__init__(chart,parent)
        self.m_isTouching=False
        self.setRubberBand(QtCharts.QChartView.RectangleRubberBand)
    def viewportEvent(self,event):
        if (event.type() == QtCore.QEvent.TouchBegin):
            # By default touch events are converted to mouse events. So
            # after this event we will get a mouse event also but we want
            # to handle touch events as gestures only. So we need this safeguard
            # to block mouse events that are actually generated from touch.
            self.m_isTouching = True

            # Turn off animations when handling gestures they
            # will only slow us down.
            # self.chart().setAnimationOptions(QtCharts.QChart.NoAnimation)
        return super().viewportEvent(event)

    def mousePressEvent(self,event):
        if (self.m_isTouching):
            return
        super().mousePressEvent(event)

    def mouseMoveEvent(self,event):
        if (self.m_isTouching):
            return;
        super().mouseMoveEvent(event);

    def mouseReleaseEvent(self,event):
        if (self.m_isTouching):
            self.m_isTouching = False;

        # Because we disabled animations when touch event was detected
        # we must put them back on.
        # self.chart().setAnimationOptions(QtCharts.QChart.SeriesAnimations);
        super().mouseReleaseEvent(event);
    def keyPressEvent(self,event):
        if(event.key()==QtCore.Qt.Key_Plus):
            self.chart().zoomIn();
        elif(event.key()==QtCore.Qt.Key_Minus):
            self.chart().zoomOut();
        elif(event.key()==QtCore.Qt.Key_Left):
            self.chart().scroll(-10, 0);
        elif(event.key()==QtCore.Qt.Key_Right):
            self.chart().scroll(10, 0);
        elif(event.key()==QtCore.Qt.Key_Up):
            self.chart().scroll(0, 10);
        elif(event.key()==QtCore.Qt.Key_Down):
            self.chart().scroll(0, -10);
        else:
            super().keyPressEvent(event);
class Chart(QtCharts.QChart):
    def __init__(self):
        super().__init__()
    # // QChart(QChart::ChartTypeCartesian, parent, wFlags),
    # // m_series(0),
    # // m_axisX(new QValueAxis()),
    # // m_axisY(new QValueAxis()),
    # // m_step(0),
    # // m_x(5),
    # // m_y(1)
        self.m_timer=QtCore.QTimer()
        self.m_timer.timeout.connect(self.handleTimeout)
        self.m_timer.setInterval(1000);
        self.m_x=5
        self.m_y=1
        self.m_series =QtCharts.QSplineSeries(self)
        green=QtGui.QPen(QtCore.Qt.red)
        green.setWidth(3);
        self.m_series.setPen(green);
        self.m_series.append(self.m_x, self.m_y);
        self.addSeries(self.m_series);

        self.m_axisX=QtCharts.QValueAxis()
        self.m_axisY=QtCharts.QValueAxis()
        self.addAxis(self.m_axisX,QtCore.Qt.AlignBottom);
        self.addAxis(self.m_axisY,QtCore.Qt.AlignLeft);
        self.m_series.attachAxis(self.m_axisX);
        self.m_series.attachAxis(self.m_axisY);
        self.m_axisX.setTickCount(5);
        self.m_axisX.setRange(0, 10);
        self.m_axisY.setRange(-5, 10);

        self.m_timer.start();

    def handleTimeout(self):
        x = self.plotArea().width() / self.m_axisX.tickCount();
        y = (self.m_axisX.max() - self.m_axisX.min()) / self.m_axisX.tickCount();
        self.m_x += y;
        # self.m_y = QtCore.QRandomGenerator().global().bounded(5) - 2.5;
        self.m_y=random.random()*5-2.5
        self.m_series.append(self.m_x, self.m_y);
        # self.scroll(x, 0);
        if (self.m_x == 100):
            self.m_timer.stop();
if __name__ == "__main__":
    a = QtWidgets.QApplication()
    a.lastWindowClosed.connect(a.quit)
    # series = QtCharts.QLineSeries()
    # for i in range(500):
    #     series.append(float(i), sin(3.14 / 50 * i) * 100)
    chart =Chart()
    # chart.setTitle("Zoom in/out example");
    chart.legend().hide();
    chartView =MyChartView(chart);
    chartView.setRenderHint(QtGui.QPainter.Antialiasing);
    window=QtWidgets.QMainWindow()
    window.setCentralWidget(chartView);
    window.resize(400, 300);
    window.grabGesture(QtCore.Qt.PanGesture);
    window.grabGesture(QtCore.Qt.PinchGesture);
    window.show()
    a.exec()